use crate::parse;

pub fn generate(parse_struct: &parse::Struct) -> u16
{
    let mut ts = Vec::<u8>::new();
    for v in &parse_struct.fields
    {
        match &v.field_type
        {
            parse::Type::I8 => ts.push(1),
            parse::Type::I16 => ts.push(2),
            parse::Type::I32 => ts.push(3),
            parse::Type::I64 => ts.push(4),
            parse::Type::I128 => ts.push(5),
            parse::Type::U8 => ts.push(6),
            parse::Type::U16 => ts.push(7),
            parse::Type::U32 => ts.push(8),
            parse::Type::U64 => ts.push(9),
            parse::Type::U128 => ts.push(10),
            parse::Type::F32 => ts.push(11),
            parse::Type::F64 => ts.push(12),
            parse::Type::Bool => ts.push(13),
            parse::Type::String => ts.push(14),
            parse::Type::Struct(_) => ts.push(15),
            parse::Type::Box(v) =>
            {
                match v.as_ref()
                {
                    parse::Type::I8 => ts.push(1),
                    parse::Type::I16 => ts.push(2),
                    parse::Type::I32 => ts.push(3),
                    parse::Type::I64 => ts.push(4),
                    parse::Type::I128 => ts.push(5),
                    parse::Type::U8 => ts.push(6),
                    parse::Type::U16 => ts.push(7),
                    parse::Type::U32 => ts.push(8),
                    parse::Type::U64 => ts.push(9),
                    parse::Type::U128 => ts.push(10),
                    parse::Type::F32 => ts.push(11),
                    parse::Type::F64 => ts.push(12),
                    parse::Type::Bool => ts.push(13),
                    parse::Type::String => ts.push(14),
                    parse::Type::Struct(_) => ts.push(15),
                    _ => ts.push(0),
                };
            }
            parse::Type::Array(v) | parse::Type::Vec(v) =>
            {
                match v.as_ref()
                {
                    parse::Type::I8 => ts.push(101),
                    parse::Type::I16 => ts.push(102),
                    parse::Type::I32 => ts.push(103),
                    parse::Type::I64 => ts.push(104),
                    parse::Type::I128 => ts.push(105),
                    parse::Type::U8 => ts.push(106),
                    parse::Type::U16 => ts.push(107),
                    parse::Type::U32 => ts.push(108),
                    parse::Type::U64 => ts.push(109),
                    parse::Type::U128 => ts.push(110),
                    parse::Type::F32 => ts.push(111),
                    parse::Type::F64 => ts.push(112),
                    parse::Type::Bool => ts.push(113),
                    parse::Type::String => ts.push(114),
                    parse::Type::Struct(_) => ts.push(115),
                    _ => ts.push(0),
                };
            }
            _ => ts.push(0),
        }
    }
    crc::Crc::<u16>::new(&crc::CRC_16_USB).checksum(ts.as_slice())
}
